<template>
    <div class="app-container">
        <div class="layout-content">
            <div class="layout-header">
                <!-- <form action="/"></form> -->
                <h5>样品扫码</h5>
                <van-row>
                    <van-col span="21">
                        <van-search v-model="searchText" placeholder="请输入搜索关键词" @search="onSearch" /></van-col>
                    <van-col span="3"><van-icon name="scan" @click="getscan" /></van-col>
                </van-row>
            </div>
            <div class="layout-main" v-if="sampleData.sampleType">
                <template v-if="sampleData.sampleType == 'sample'">
                    <van-cell title="样品编号" :value="sampleData.sampleCode" />
                    <van-cell title="样件名称" :value="sampleData.sampleName" />
                    <van-cell title="零部件号或发动机号" :value="sampleData.sampleModel" />
                    <van-cell title="生产厂家" :value="sampleData.deliveryOrg" />
                    <van-cell title="委托单号" :value="sampleData.projectId_dictText" />
                    <van-cell title="所属项目" :value="sampleData.projectName" />
                    <van-cell title="试验项目" :value="sampleData.itemName" />
                    <!-- <van-cell title="样品状态" :value="sampleData.sampleState_dictText" /> -->
                    <van-cell title="样品状态" :value="sampleData.sampleState_dictText | sampleFilter" />
                    <van-cell title="下线编号" :value="sampleData.offlineNo" />
                </template>
                <template v-else>
                    <van-cell title="样品编号" :value="sampleData.sampleCode" />
                    <van-cell title="车辆名称" :value="sampleData.sampleName" />
                    <van-cell title="车型" :value="sampleData.carModel_dictText" />
                    <van-cell title="VIN" :value="sampleData.sampleModel" />
                    <van-cell title="生产厂家" :value="sampleData.deliveryOrg" />
                    <van-cell title="委托单号" :value="sampleData.projectId_dictText" />
                    <van-cell title="所属项目" :value="sampleData.projectName" />
                    <van-cell title="试验项目" :value="sampleData.itemName" />
                    <!-- <van-cell title="样品状态" :value="sampleData.sampleState_dictText" /> -->
                    <van-cell title="样品状态" :value="sampleData.sampleState_dictText | sampleFilter" />
                    <van-cell title="下线编号" :value="sampleData.offlineNo" />
                </template>
                <van-row style="margin-top: 50px">
                    <van-col span="12" style="text-align: center">
                        <van-button type="info" @click="goSampleuse">样品领样</van-button>
                    </van-col>
                    <van-col span="12" style="text-align: center">
                        <van-button @click="goReturnSample" type="info">样品还样</van-button>
                    </van-col>
                </van-row>
            </div>
        </div>

        <!-- <div class="layout-footer">
      <TabBar :activeTab="2"></TabBar>
    </div> -->
        <Scan @getcode="getprojectCode" ref="scan"></Scan>
    </div>
</template>

<script>
import { Notify } from "vant";
import TabBar from "@/components/TabBar";
import { getAction } from "@/api/manage";
import Scan from "../components/Scan.vue";
export default {
    data () {
        return {
            searchText: "",
            sampleData: {},
            url: {
                list: "lims/limsProjectSample/list",
                queryById: "lims/limsProjectSample/queryById",
            },
        };
    },
    components: {
        TabBar,
        Scan,
    },
    created () { },
    filters: {
        sampleFilter (val) {
            console.log(val);
            // let obj = { 待收样: "待检", 领样: "在检", 在库: "检毕" };
            let arr = [
                { key: "待收样", title: "待检" },
                { key: "领样", title: "在检" },
                { key: "在库", title: "检毕" },
                { key: "领用", title: "在检" },
            ];
            return arr.find((item) => item.key == val)
                ? arr.find((item) => item.key == val).title
                : val;
        },
    },
    methods: {
        getprojectCode (val) {
            console.log(val, "val");
            this.searchText = val;
            this.onSearch(val);
        },
        getscan () {
            this.$refs.scan.scanCode();
        },
        goSampleuse () {
            let params = {
                tableId: this.sampleData.id,
            };

            if (this.sampleData.sampleType == "sample") {
                params.processKey = "lims_sample_borrow";
            } else {
                params.processKey = "lims_sample_car_borrow";
            }
            // lims_sample_borrow
            // lims_sample_car_borrow
            this.$router.push({ path: "/sampleUse", query: params });
        },
        goReturnSample () {
            // lims_sample_return
            // lims_sample_car_return
            let params = {
                tableId: this.sampleData.id,
            };

            if (this.sampleData.sampleType == "sample") {
                params.processKey = "lims_sample_return";
            } else {
                params.processKey = "lims_sample_car_return";
            }
            this.$router.push({ path: "/returnSample", query: params });
        },
        onSearch (val) {
            console.log(val);
            this.getSample(val);
        },
        getSample (val) {
            if (!val) {
                return;
            }
            let params = {
                sampleCode: val,
            };
            getAction(this.url.list, params).then((res) => {
                console.log(res);
                if (res.success && res.result.records.length) {
                    console.log(res.result.records[0].id);
                    getAction(this.url.queryById, { id: res.result.records[0].id }).then(
                        (data) => {
                            console.log(data);
                            this.sampleData = data.result;
                        }
                    );
                } else if (!res.result.records.length) {
                    this.sampleData = {};
                    Notify({ type: "warning", message: "暂无样品信息" });
                } else {
                    Notify({ type: "warning", message: res.message });
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.app-container {
    background-color: #f5f5f5;
    height: 100%;

    .layout-content {
        .layout-header {
            padding-top: 20px;
            background-color: #fff;

            h5 {
                margin: 0;
                font-size: 18px;
                margin-left: 10px;
            }

            .van-row {
                .van-col {
                    text-align: center;

                    .van-icon {
                        margin-top: 10px;
                    }
                }
            }
        }

        .layout-main {
            .van-cell {
                // border-bottom: 1px solid #ccc;
            }

            .van-button {
                width: 70%;
                border-radius: 5px;
            }
        }
    }
}
</style>
